//
//  JQAppDelegate.m
//  JQLive
//
//  Created by Bruce on 2020/8/5.
//  Copyright © 2020 mlqk. All rights reserved.
//

#import "JQAppDelegate.h"
#import "JQLaunchAdPageHUD.h"
#import "BaseTabBarViewController.h"
#import "AFNetworkReachabilityManager.h"

@interface JQAppDelegate ()

@end

@implementation JQAppDelegate

// 当程序载入后执行
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"当程序载入后执行");
    // Override point for customization after application launch.
    
    // 配置
    [self startMonitoringNetwork];
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    // 网络图片 不论 gif 或者 jpg 都存在加载不出来的情况
    NSString *adimageGIFUrl = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1596706453936&di=2e037fb69785cdae2e0c8d757efc3bb3&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201702%2F18%2F20170218161710_wiVcE.gif";
//    // 本地加载图片 支持 gif jpg
//    NSString *adImageGifPath = [[NSBundle mainBundle] pathForResource:@"adImage" ofType:@"gif"];
//    // 加载 Assets.xcassets的存在图片
//    NSString *imageUrl = @"Launch_nineLivebar";
    
    // aDduration 等待跳过时间+1s
    // aDImageUrl 网络图片或本地图片
    // hideSkipButton是否隐藏跳过按钮
    // launchAdClickBlock 回调
//    [JQLaunchAdPageHUD instanceWithFrame:CGRectMake(0, 0, DDScreenW, DDScreenH-136) aDduration:4.0 aDImageUrl:adimageGIFUrl hideSkipButton:NO launchAdClickBlock:^{
//        NSLog(@"[JQAppDelegate]:点了广告图片");
////        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.baidu.com"]];
////        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.baidu.com"] options:@{} completionHandler:^(BOOL success) {
//        }];
//    NSString *str = @"https://www.baidu.com";
//    MYSimpleWebVC *vc = [[MYSimpleWebVC alloc] initWithURLString:str];
//    [self.navigationController pushViewController:vc animated:YES];
//    }];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

//    self.window.rootViewController = [[UIViewController alloc] init];
    self.window.rootViewController = [[BaseTabBarViewController alloc] init];
    return YES;
}
// 网络监测
- (void)startMonitoringNetwork{
    AFNetworkReachabilityManager *manger = [AFNetworkReachabilityManager sharedManager];
    [manger startMonitoring];
    [manger setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
            {
                //未知网络
            }
                break;
            case AFNetworkReachabilityStatusNotReachable:
            {
                //没有网络
                [MYTipUtils showAlertString:@"网络连接失败，稍后再试"];
            }
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
            {
                //WWAN
            }
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
            {
                //Wi-Fi
            }
                break;
            default:
                break;
        }
    }];
}
// 应用程序将要进入非活动状态，即将进入后台
-(void)applicationWillResignActive:(UIApplication *)application{
    NSLog(@"应用程序将要进入非活动状态，即将进入后台");
}
// 如果应用程序支持后台运行，则应用程序已经进入后台运行
- (void)applicationDidEnterBackground:(UIApplication *)application{
    NSLog(@"如果应用程序支持后台运行，则应用程序已经进入后台运行");
}
// 应用程序将要进入活动状态，即将进入前台运行
-(void)applicationWillEnterForeground:(UIApplication *)application{
    NSLog(@"应用程序将要进入活动状态，即将进入前台运行");
}
// 应用程序已进入前台，处于活动状态
-(void)applicationDidBecomeActive:(UIApplication *)application{
    NSLog(@"应用程序已进入前台，处于活动状态");
}
// 应用程序将要退出，通常用于保存数据和一些退出前的清理工作
-(void)applicationWillTerminate:(UIApplication *)application{
    NSLog(@"应用程序将要退出，通常用于保存数据和一些退出前的清理工作");
}
// 系统内存不足，需要进行清理工作
-  (void)applicationDidReceiveMemoryWarning:(UIApplication *)application{
    NSLog(@"系统内存不足，需要进行清理工作");
}
// 当系统时间发生改变时执行
- (void)applicationSignificantTimeChange:(UIApplication *)application{
    NSLog(@"当系统时间发生改变时执行");
}
// 已被弃用 代替方法为启动方法 application: didFinishLaunchingWithOptions:
/*
-(void)applicationDidFinishLaunching:(UIApplication *)application{
    NSLog(@"6");
}
*/

#pragma mark - Core Data stack

@synthesize persistentContainer = _persistentContainer;

- (NSPersistentCloudKitContainer *)persistentContainer {
    // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
    @synchronized (self) {
        if (_persistentContainer == nil) {
            _persistentContainer = [[NSPersistentCloudKitContainer alloc] initWithName:@"JQLive"];
            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
                if (error != nil) {
                    // Replace this implementation with code to handle the error appropriately.
                    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                    
                    /*
                     Typical reasons for an error here include:
                     * The parent directory does not exist, cannot be created, or disallows writing.
                     * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                     * The device is out of space.
                     * The store could not be migrated to the current model version.
                     Check the error message to determine what the actual problem was.
                    */
                    NSLog(@"Unresolved error %@, %@", error, error.userInfo);
                    abort();
                }
            }];
        }
    }
    
    return _persistentContainer;
}

#pragma mark - Core Data Saving support

- (void)saveContext {
    NSManagedObjectContext *context = self.persistentContainer.viewContext;
    NSError *error = nil;
    if ([context hasChanges] && ![context save:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, error.userInfo);
        abort();
    }
}

@end
